#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
#include <QNetworkInterface>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    QList<QHostAddress> list = QNetworkInterface::allAddresses();
//    foreach (QHostAddress address, list) {
//        if (!address.isNull())
//            qDebug() << "Address : " << address.toString();
//    }
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface netInterface, list) {
        if (!netInterface.isValid())
            continue;

        qDebug() << "********************";

        QNetworkInterface::InterfaceFlags flags = netInterface.flags();
        if (flags.testFlag(QNetworkInterface::IsRunning)
                    && !flags.testFlag(QNetworkInterface::IsLoopBack)) {  // 网络接口处于活动状态
            qDebug() << "Device : " << netInterface.name();  // 设备名
            qDebug() << "HardwareAddress : " << netInterface.hardwareAddress();  // 硬件地址
            qDebug() << "Human Readable Name : " << netInterface.humanReadableName();  // 人类可读的名字
        }

         QList<QNetworkAddressEntry> entryList = netInterface.addressEntries();
         foreach(QNetworkAddressEntry entry, entryList) { // 遍历每一个IP地址
             qDebug() << "IP Address:" << entry.ip().toString();  // IP地址
             qDebug() << "Netmask:" << entry.netmask().toString();  // 子网掩码
             qDebug() << "Broadcast:" << entry.broadcast().toString();  // 广播地址
        }
    }
}


MainWindow::~MainWindow()
{
    delete ui;
}
